#ifndef REMOTEMEDIASTREAM_H
#define REMOTEMEDIASTREAM_H

#include <QObject>
#include "MediaStream.h"
#include "api/peer_connection_interface.h"
class JanusPeerConnection;

class DummySetSessionDescriptionObserver
    : public webrtc::SetSessionDescriptionObserver
{
public:
    static DummySetSessionDescriptionObserver* Create()
    {
        return new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
    }
    virtual void OnSuccess()
    {
        RTC_LOG(INFO) << __FUNCTION__;
    }

    virtual void OnFailure(webrtc::RTCError error)
    {
        RTC_LOG(INFO) << __FUNCTION__ << " " << ToString(error.type()) << ": "
            << error.message();
    }
};

class RemoteMediaStream
        : public QObject,
        public MediaStream,
        public webrtc::PeerConnectionObserver,
        public webrtc::CreateSessionDescriptionObserver
{
    Q_OBJECT
public:
    explicit RemoteMediaStream(const qint64& peerID, QObject *parent = nullptr);
    virtual ~RemoteMediaStream();
    void setPeerConnection(JanusPeerConnection* peer);

    void connectToPeer(const bool& dtls);

    // 以下为从WebRtc接口继承过来的函数
public:
    // Triggered when the SignalingState changed.
    virtual void OnSignalingChange(
        webrtc::PeerConnectionInterface::SignalingState new_state);

    // Triggered when media is received on a new stream from remote peer.
    virtual void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);

    // Triggered when a remote peer closes a stream.
    virtual void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream);

    // Triggered when a remote peer opens a data channel.
    virtual void OnDataChannel(
        rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel);

    // Triggered when renegotiation is needed. For example, an ICE restart
    // has begun.
    virtual void OnRenegotiationNeeded();

    // Called any time the legacy IceConnectionState changes.
    //
    // Note that our ICE states lag behind the standard slightly. The most
    // notable differences include the fact that "failed" occurs after 15
    // seconds, not 30, and this actually represents a combination ICE + DTLS
    // state, so it may be "failed" if DTLS fails while ICE succeeds.
    //
    // TODO(jonasolsson): deprecate and remove this.
    virtual void OnIceConnectionChange(
        webrtc::PeerConnectionInterface::IceConnectionState new_state);

    // Called any time the standards-compliant IceConnectionState changes.
    virtual void OnStandardizedIceConnectionChange(
        webrtc::PeerConnectionInterface::IceConnectionState new_state);

    // Called any time the PeerConnectionState changes.
    virtual void OnConnectionChange(
        webrtc::PeerConnectionInterface::PeerConnectionState new_state);

    // Called any time the IceGatheringState changes.
    virtual void OnIceGatheringChange(
        webrtc::PeerConnectionInterface::IceGatheringState new_state);;

    // A new ICE candidate has been gathered.
    virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate);;

    // Gathering of an ICE candidate failed.
    // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror
    // |host_candidate| is a stringified socket address.
    virtual void OnIceCandidateError(const std::string& host_candidate,
                                     const std::string& url,
                                     int error_code,
                                     const std::string& error_text);

    // Gathering of an ICE candidate failed.
    // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror
    virtual void OnIceCandidateError(const std::string& address,
                                     int port,
                                     const std::string& url,
                                     int error_code,
                                     const std::string& error_text);

    // Ice candidates have been removed.
    // TODO(honghaiz): Make this a pure virtual method when all its subclasses
    // implement it.
    virtual void OnIceCandidatesRemoved(
        const std::vector<cricket::Candidate>& candidates);

    // Called when the ICE connection receiving status changes.
    virtual void OnIceConnectionReceivingChange(bool receiving);

    // Called when the selected candidate pair for the ICE connection changes.
    virtual void OnIceSelectedCandidatePairChanged(
        const cricket::CandidatePairChangeEvent& event);

    // This is called when a receiver and its track are created.
    // TODO(zhihuang): Make this pure virtual when all subclasses implement it.
    // Note: This is called with both Plan B and Unified Plan semantics. Unified
    // Plan users should prefer OnTrack, OnAddTrack is only called as backwards
    // compatibility (and is called in the exact same situations as OnTrack).
    virtual void OnAddTrack(
        rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
        const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>& streams);

    // This is called when signaling indicates a transceiver will be receiving
    // media from the remote endpoint. This is fired during a call to
    // SetRemoteDescription. The receiving track can be accessed by:
    // |transceiver->receiver()->track()| and its associated streams by
    // |transceiver->receiver()->streams()|.
    // Note: This will only be called if Unified Plan semantics are specified.
    // This behavior is specified in section 2.2.8.2.5 of the "Set the
    // RTCSessionDescription" algorithm:
    // https://w3c.github.io/webrtc-pc/#set-description
    virtual void OnTrack(rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver);

    // Called when signaling indicates that media will no longer be received on a
    // track.
    // With Plan B semantics, the given receiver will have been removed from the
    // PeerConnection and the track muted.
    // With Unified Plan semantics, the receiver will remain but the transceiver
    // will have changed direction to either sendonly or inactive.
    // https://w3c.github.io/webrtc-pc/#process-remote-track-removal
    // TODO(hbos,deadbeef): Make pure virtual when all subclasses implement it.
    virtual void OnRemoveTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver);

    // Called when an interesting usage is detected by WebRTC.
    // An appropriate action is to add information about the context of the
    // PeerConnection and write the event to some kind of "interesting events"
    // log function.
    // The heuristics for defining what constitutes "interesting" are
    // implementation-defined.
    virtual void OnInterestingUsage(int usage_pattern);


    virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc);
    // The OnFailure callback takes an RTCError, which consists of an
    // error code and a string.
    // RTCError is non-copyable, so it must be passed using std::move.
    // Earlier versions of the API used a string argument. This version
    // is removed; its functionality was the same as passing
    // error.message.
    virtual void OnFailure(webrtc::RTCError error);

Q_SIGNALS:

private:
    qint64                  m_peerID;
    rtc::scoped_refptr<webrtc::PeerConnectionInterface>     m_peerConnection;
    JanusPeerConnection*                                    m_pJanusPeerConnection;


};

#endif // REMOTEMEDIASTREAM_H
